<?php

/** Zend_Controller_Action */
require_once ROOT_PATH . '/library/Soooner/Controller/WebAction.php';

class StaticController extends Soooner_Web_Controller_Action
{
	private $dir = "";
	private $bizsite = "http://127.0.0.1";
	public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();  
    	$this->dir = APPLICATION_PATH."/../public/iface/";
    }
	public function indexAction()
    {
        echo $this->dir; exit;
    }
    public function configAction()
    {
    	$modelTypes = new Model_Tables_Types();
    	$types=$modelTypes->getList(1,100);
    	foreach($types as $info){
    		system("wget '$this->bizsite/iiface/config/id/".$info["id"]."' --save-cookies=cookies --keep-session-cookies -O ".$this->dir."config_".$info["id"].".xml");
    	}
    	system("wget '$this->bizsite/iiface/config/' --save-cookies=cookies --keep-session-cookies -O ".$this->dir."config.xml");
    	echo "/iface/config.xml";
    }
    public function searchAction()
    {
    	header("Content-Type: text/xml");
    	$header="<?xml version='1.0' encoding='utf-8' ?>\n";
        include $this->_dzTemplate->getFile( 'web/list.html' );
    }
    
    public function bestAction(){
    	$typeid = $this->_getParam('typeid');
    	$tag1 = $this->_getParam('tag1');
    	$tag2 = $this->_getParam('tag2');
    	$tag3 = $this->_getParam('tag3');
    	//$options = $GLOBALS['application']->getOptions();
    	$pageCount = 5;//$options["listcount"];//每页显示的条数  	
    	$modelTypes = new Model_Tables_Types();
    	$typeInfo = $modelTypes->getInfo($typeid);
    	if($typeInfo["model"]=="vod"){
    		$modelVod = new Model_Tables_Vod();
    		$count = $modelVod->getCountbyTags($typeid,$tag1,$tag2,$tag3);
    		$pnum = ceil($count/$pageCount);
    	}else if($typeInfo["model"]=="live"){
    		$modelLive = new Model_Tables_Live();
    		$count = $modelLive->getCountbyTags($typeid,$tag1,$tag2,$tag3);
    		$pnum = ceil($count/$pageCount);    		
    	}
    	$bestpath = $this->dir."ipadlist/$typeid";
    	if (!file_exists($bestpath)){
    			system("mkdir -p $bestpath");}
    	system("wget '$this->bizsite/iiface/list/typeid/$typeid/tag1/$tag1/tag2/$tag2/tag3/$tag3/ipad/1/index/1/pcount/5' --save-cookies=cookies --keep-session-cookies -O ".$bestpath."/best.xml");
    	echo "/iface/ipadlist/$typeid/best.xml";
    }
    public function listAction(){
    	$typeid = $this->_getParam('typeid');
    	$tag1 = $this->_getParam('tag1');
    	$tag2 = $this->_getParam('tag2');
    	$tag3 = $this->_getParam('tag3');
    	$options = $GLOBALS['application']->getOptions();
    	$pageCount = $options["listcount"];//每页显示的条数  	
    	$modelTypes = new Model_Tables_Types();
    	$typeInfo = $modelTypes->getInfo($typeid);
    	if($typeInfo["model"]=="vod"){
    		$modelVod = new Model_Tables_Vod();
    		$count = $modelVod->getCountbyTags($typeid,$tag1,$tag2,$tag3);
    		$pnum = ceil($count/$pageCount);
    	}else if($typeInfo["model"]=="live"){
    		$modelLive = new Model_Tables_Live();
    		$count = $modelLive->getCountbyTags($typeid,$tag1,$tag2,$tag3);
    		$pnum = ceil($count/$pageCount);    		
    	}
    	$path = $this->dir."list/$typeid/$tag1/$tag2/$tag3";
    	$ipadpath = $this->dir."ipadlist/$typeid/$tag1/$tag2/$tag3";
    	if (!file_exists($path)){
    			system("mkdir -p $path");}	
    	if (!file_exists($ipadpath)){
    			system("mkdir -p $ipadpath");}	
    	system("wget '$this->bizsite/iiface/list/typeid/$typeid/tag1/$tag1/tag2/$tag2/tag3/$tag3/ipad/0/index/1' --save-cookies=cookies --keep-session-cookies -O ".$path."/1.xml");		
    	system("wget '$this->bizsite/iiface/list/typeid/$typeid/tag1/$tag1/tag2/$tag2/tag3/$tag3/ipad/1/index/1' --save-cookies=cookies --keep-session-cookies -O ".$ipadpath."/1.xml");
    	for($index=2;$index<=$pnum;$index++){
    		system("wget '$this->bizsite/iiface/list/typeid/$typeid/tag1/$tag1/tag2/$tag2/tag3/$tag3/ipad/0/index/$index' --save-cookies=cookies --keep-session-cookies -O ".$path."/$index.xml");
    		system("wget '$this->bizsite/iiface/list/typeid/$typeid/tag1/$tag1/tag2/$tag2/tag3/$tag3/ipad/1/index/$index' --save-cookies=cookies --keep-session-cookies -O ".$ipadpath."/$index.xml");
    	}
    	echo "/iface/list/$typeid/$tag1/$tag2/$tag3/1.xml";
    }
    
    public function liveAction(){
    	$vid = $this->_getParam('vid');
    	$d = $this->_getParam('d');//日期
    	$options = $GLOBALS['application']->getOptions();
    	$pageCount = $options["livelistcount"];//每页显示的条数
    	$modelLive = new Model_Tables_Live();
    	$info = $modelLive->getInfo($vid);
    	if($info["ists"]==1){
    		$modelEpg = new Model_Tables_Epg();
    		//$count = $modelEpg->getCount($d,$vid,2);
    		//$pnum = ceil($count/$pageCount);
    		$pnum = 3;//生成3天的节目单
    	}
    	$path = $this->dir."live/$vid";
    		if (!file_exists($path)){
    			system("mkdir -p $path");}	
    	$saveday = date('Y-m-d',strtotime($d));//
    	system("wget '$this->bizsite/iiface/live/vid/$vid/d/$d/index/1/' --save-cookies=cookies --keep-session-cookies -O ".$path."/1.xml");
    	system("\\cp -f ".$path."/1.xml ".$path."/".$saveday.".xml");	
     	for($index=0;$index<=$pnum;$index++){
     		//计算日期
     		$saveday = date('Y-m-d',strtotime($d)-($index-1)*24*60*60);//
    		$path = $this->dir."live/$vid";
    		if (!file_exists($path)){
    			system("mkdir -p $path");}	    
    		system("wget '$this->bizsite/iiface/live/vid/$vid/d/$d/index/$index/' --save-cookies=cookies --keep-session-cookies -O ".$path."/".$index.".xml");
    		system("\\cp -f ".$path."/".$index.".xml ".$path."/".$saveday.".xml");
    	}
    	echo "/iface/live/$vid/1".".xml";
    }
    
    public function vodAction(){
    	$vid = $this->_getParam('vid');    	
    	$options = $GLOBALS['application']->getOptions();
    	$pageCount = $options["vodlistcount"];//每页显示的条数
    	$modelVod = new Model_Tables_Vod();
    	$info = $modelVod->getInfo($vid);
    	//if($info["state"]>1){
    		$modelVodlist = new Model_Tables_Vodlist();
    		$count = $modelVodlist->getCount("",$vid);
    		$info["state"] = $count;
    		$pnum = ceil($count/$pageCount);
    	//}
    	$path = $this->dir."vod/$vid";
    	if (!file_exists($path)){
    		system("mkdir -p $path");}		    
    	system("wget '$this->bizsite/iiface/vod/vid/$vid/index/1/' --save-cookies=cookies --keep-session-cookies -O ".$path."/1.xml");
    	
    	for($index=1;$index<=$pnum;$index++){
     		system("wget '$this->bizsite/iiface/vod/vid/$vid/index/$index/' --save-cookies=cookies --keep-session-cookies -O ".$path."/$index.xml");
    	}
    	echo "/iface/vod/$vid/1.xml";
    }
    
    public function createlistAction(){
    	$typeid = $this->_getParam('typeid');
    	$modelTypes=new Model_Tables_Types();
    	$typeInfos = $modelTypes->getList(1,200,"");
    	$infos0=$modelTypes->getList(1,200);
    	foreach($infos0 as $info){
    		if($info["id"]==$typeid ){
    			$info["tag1"]=$modelTypes->parseTag($info["tag1"],true);
	    		$info["tag2"]=$modelTypes->parseTag($info["tag2"],true);
	    		$info["tag3"]=$modelTypes->parseTag($info["tag3"],true);
	    		$infos[]=$info;
    		}
    	}
    	if($typeid)
    		$info=json_encode($infos[0]);
    	include $this->_dzTemplate->getFile( 'web/createlist.html' );
    }
    public function createdetailAction(){//详细信息
    	$typeid = $this->_getParam('typeid');
    	$modelTypes = new Model_Tables_Types();
    	$typeInfos = $modelTypes->getList(1,200,"");
    	$typeInfo = $modelTypes->getInfo($typeid);    	
    	if($typeInfo["model"]=="vod"){
    		$modelVod = new Model_Tables_Vod();
    		$infos["vod"]=$modelVod->getIds($typeid);
    	}else if($typeInfo["model"]=="live"){
    		$modelLive = new Model_Tables_Live();
    		$infos["live"]=$modelLive->getIds($typeid);
    	}
    	if($typeid)
    		$info=json_encode($infos);
    	else $info=json_encode(array());
    	include $this->_dzTemplate->getFile( 'web/createdetail.html' );
    }
}